from flask import Blueprint, request, jsonify
from app import db
from app.models.user import User

users_bp = Blueprint('users', __name__, url_prefix='/users')

# 获取所有用户
@users_bp.route('', methods=['GET'])
def get_users():
    users = User.query.all()
    return jsonify([user.to_dict() for user in users]), 200

# 更新用户信息（现在只能更新用户名）
@users_bp.route('/updatePwd', methods=['POST'])
def update_user():
    data = request.get_json()
    user = User.query.get_or_404(data['userId'])
    
    # 如果需要更新密码，可以添加密码更新逻辑
    user.set_password(data['newPassword'])
    
    db.session.add(user)
    db.session.commit()
    return jsonify({
        'message': '用户信息更新成功',
        'user': user.to_dict()
    }), 200
